import { statusColor } from "app/constants/statusColor"
import { parse, format } from "date-fns"

export const formattedTime = (inputTime: any) => {
  const parsedTime = parse(inputTime, "h:mm a", new Date())
  // Format it in the desired format, for example, 'HH:mm:ss.SSS'
  const formattedTime = format(parsedTime, "HH:mm:ss")
  return formattedTime
}

// converting from this 13:00:00 ti this 01:00 AM format
export const convertIn12HourFormat = (inputTime: any) => {
  const parsedTime = parse(inputTime, "HH:mm:ss", new Date())
  // Format it in the desired format, for example, 'HH:mm:ss.SSS'
  const formattedTime = format(parsedTime, "h:mm a")
  return formattedTime
}

export const unixDateConverter = (date: string) => {
  const [day, month, year] = date.split("/").map(Number)
  const unixDate = new Date(year, month - 1, day) // Month is zero-based, so subtract 1
  const unixTimestamp = Math.floor(unixDate.getTime() / 1000)
  return unixTimestamp
}

export const eventDateTimeConverter = (eventData: any) => {
  const startDate = new Date(parseInt(eventData.start_date) * 1000)
  const startTimeParts = eventData.start_time.split(":")
  const endTimeParts = eventData.end_time.split(":")

  const start = new Date(
    startDate.getFullYear(),
    startDate.getMonth(),
    startDate.getDate(),
    parseInt(startTimeParts[0]),
    parseInt(startTimeParts[1]),
    parseInt(startTimeParts[2]),
  )

  const end = new Date(
    startDate.getFullYear(),
    startDate.getMonth(),
    startDate.getDate(),
    parseInt(endTimeParts[0]),
    parseInt(endTimeParts[1]),
    parseInt(endTimeParts[2]),
  )

  return {
    title: "Meeting",
    start: start,
    end: end,
    status: eventData?.status,
    color: statusColor[eventData?.status],
    startHour: eventData?.start_time?.slice(0, -3) + " to " + eventData?.end_time?.slice(0, -3),
    id: eventData?.id,
    booking: eventData?.booking ? true : false,
    clients: eventData?.clients,
  }
}
